angular.module('myApp', ['infinite-scroll']);

function hotSales($scope, $http) {
	var mid = readData(localStorage.marketId);
	//$scope.Name = readData(localStorage.marketName);
	$scope.Name = '热卖商品';
	var allgoodsUrl = getUrl("88hots");
	var requestMain = {};
   	$scope.pages = 0; //默认加载第一页，初始值0
	$scope.totalpage = 1; //默认总页数
	$scope.goodsAll = []; //商品列表	
	$scope.all_products = function() { //热卖商品
		if ($scope.pages <= $scope.totalpage) {
			$scope.loginJudge();
			$scope.pages = $scope.pages + 1;
			$http(requestMain).success(function(data, status, headers, config) {
				console.log(JSON.stringify(data))
				if (data.obj) {
					var goodsList = data.obj.shopHots;
					for (var i = 0; i < goodsList.length; i++) {
						if (!goodsList[i].activity) {
							goodsList[i].activityshow = false;
						} else {
							goodsList[i].activityshow = true;
						}
						$scope.goodsAll.push(goodsList[i]);
					}
					$scope.totalpage = data.obj.totalCount / 7;
				} else {
					mui.toast('暂时还没有热卖商品吆~~')
					$scope.goodsAll = [];
					$scope.pages = $scope.pages - 1;
				};
			}).error(function(er) {
				$scope.pages = $scope.pages - 1;
				console.log('热卖商品error' + er);
			});
		}
	}

	$scope.detail = function(alls) {
		//alert(JSON.stringify(alls))
		if (getlogin('../my/loginHead.html')) {
			if (alls.circle.attention) { //打开商品详情页
				openProductDetail({
					urls: 'productDetail.html',
					goodId: alls.productId,
					guideId: alls.circle.userId,
					circleName: alls.circle.circleName,
					groupId: alls.circle.noticeGroupId
				});
			} else { //打开圈子详情页
				openGuanzhu({
					uid: alls.circle.userId,
					urls: 'guanzhu.html'
				});
			}
		}
	}
	$scope.loginJudge = function() {
		if (localStorage.account) { //已登录
			allgoodsUrl = getUrl("88hots");
			var headdata = getHeader();
			var aid = readData(localStorage.account);
			requestMain = {
				method: 'POST',
				url: allgoodsUrl,
				headers: headdata,
				timeout: 10000,
				params: {
					marketId: mid,
					accountId: aid,
					pageIndex: $scope.pages,
					pageSize: 7
				}
			};
		} else { //未登录
			requestMain = {
				method: 'POST',
				url: allgoodsUrl,
				timeout: 10000,
				params: {
					marketId: mid,
					pageIndex: $scope.pages,
					pageSize: 7
				}
			};
		}
	}
	window.addEventListener("reloads", function() {
		$scope.pages = 0; //默认加载第一页，初始值0
		$scope.totalpage = 1; //默认总页数
		$scope.goodsAll = []; //商品列表	
		$scope.all_products()
	})
}